#!/usr/bin/env python
#
#   urldb.py - URL database
#   
#
import sys
import bz2, binascii
import sqlite3

class UrlDB:
    '''
    Database of URLs
    '''
    def __init__(self, urldb):
        '''
        Initialize database 
        
        Every URL database exists in separate sqlite database
        '''
        self.__conn = sqlite3.connect(urldb)
        self.__cursor = self.__conn.cursor()
        SQL = """ 
        CREATE TABLE IF NOT EXISTS urldb (
            id INTEGER PRIMARY KEY,
            url TEXT UNIQUE,
            type TEXT,
            status TEXT,
            md5 TEXT,
            content TEXT
        );
        """
        self.__cursor.executescript(SQL)

    def filter(self, **kwargs):
        '''
        select URLs by filter
        '''
        return None

    def select(self, where=None, limit=0):
        '''
        select urls
        
        row: id, url, type, status, md5, content
        '''
        data = []
        SQL = 'SELECT * FROM urldb'
        if where: SQL += " WHERE %s" % where
        if limit > 0: SQL += " LIMIT %d" % limit
        self.__cursor.execute(SQL)
        for row in self.__cursor:
            data.append(row)
        return data
        
    def add(self, url, **kwargs):
        '''
        add URL to database
        
        content field is compressed by bz2 
        '''
        p_names = ''
        p_values = ''
        for param in kwargs.keys():
            if kwargs[param]:
                p_names += ',' + param
                if param == 'content':
                    p_values += ',"' + binascii.hexlify(bz2.compress(kwargs[param])) + '"'
                else:
                    p_values += ',"' + kwargs[param] + '"'
        
        SQL = 'INSERT INTO urldb (url%s) VALUES ("%s"%s)' % (p_names, url, p_values)
        try:
            self.__cursor.execute(SQL)
        except sqlite3.IntegrityError:
            pass

    def update(self, url, **kwargs):
        '''
        update URL in database
        '''
        params = ''
        for param in kwargs.keys():
            if kwargs[param]:
                if param == 'content':
                    params += param + '="' + binascii.hexlify(bz2.compress(kwargs[param])) + '",'
                else:
                    params += param + '="' + kwargs[param] + '",'
            else:
                params += param + '=Null,'
        if len(params) > 0: params = params[:-1]
        SQL = 'UPDATE urldb SET %s WHERE url = "%s"' % (params, url)
        self.__cursor.execute(SQL)
        
    def remove(self, url):
        '''
        remove URL from database
        '''
        return None

    def sync(self):
        '''
        sync data wyth disk: commit transactions
        '''
        self.__conn.commit()

        
if __name__ == "__main__":

    from optparse import OptionParser
    import sys
	
    parser = OptionParser()
    parser.add_option("-d", "--db", dest="db", help="url database file (sqlite3)")
    (options, args) = parser.parse_args()

    if options.db == None:
        parser.error("Incorrect number of arguments, \n\nuse -h or --help for detail\n")
        sys.exit()

    urls_db = UrlDB(options.db)
